package com.glls.stream;

import com.glls.lambda.pojo.Student;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * @date 2022/10/4
 * @desc Stream 流的终止操作
 *
 *  查找与匹配
 */
public class StreamDemo4 {

    List<Student> list = Arrays.asList(
            new Student(1,"zs",18,88.0),
            new Student(2,"ls",19,88.5),
            new Student(3,"ww",21,98.0),
            new Student(4,"zl",19,86.0),
            new Student(5,"tq",17,92.0),
            new Student(5,"tq",17,92.0)
    );

    @Test
    public void test(){
        // allMatch
        // anyMatch
        // noneMatch
        boolean b = list.stream()
                .allMatch((s) -> s.getScore() > 80);
        System.out.println(b);
    }

    @Test
    public void test2(){
        //findFirst
        //Optional<Student> first = list.stream().findFirst();
        //
        //System.out.println(first.get());
        // 扩展  得到成绩最高的人的名字
        Optional<String> first1 = list.stream()
                .sorted((s1, s2) -> {
                    return s1.getScore() > s2.getScore() ? -1 : 1;
                })
                .map(Student::getName)
                .findFirst();

        System.out.println(first1.get());
    }


    @Test
    public void test3(){
        //count
        long count = list.stream().filter(s -> s.getScore() > 90).count();
        System.out.println(count);
    }

    @Test
    public void test4(){
        //max   min
        //Optional<Student> max = list.stream().max((s1, s2) -> s1.getScore() - s2.getScore() > 0 ? 1 : -1);
        Optional<Student> max = list.stream().min((s1, s2) -> s1.getScore() - s2.getScore() > 0 ? 1 : -1);

        System.out.println(max.get());

        Optional<Double> min = list.stream().map(Student::getScore).min(Double::compareTo);

        System.out.println(min.get());    // 唯手熟尔
    }
}
